<?php   
class ControllerCommonHeader extends Controller {
	protected function index() {
        $this->load->model('tool/seo_url'); 
        
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['language_code'])) {
			$this->session->data['language'] = $this->request->post['language_code'];
		
			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->model_tool_seo_url->rewrite($this->request->post['redirect']));
			} else {
				$this->redirect($this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home'));
			}
    	}		
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['currency_code'])) {
      		$this->currency->set($this->request->post['currency_code']);
			
			unset($this->session->data['shipping_methods']);
			unset($this->session->data['shipping_method']);
				
			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->model_tool_seo_url->rewrite($this->request->post['redirect']));
			} else {
				$this->redirect($this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home'));
			}
   		}
		
		$this->language->load('common/header');
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = HTTPS_IMAGE;
		} else {
			$server = HTTP_IMAGE;
		}

		$this->data['title'] = $this->document->title;
		$this->data['keywords'] = $this->document->keywords;
		$this->data['description'] = $this->document->description;
		$this->data['template'] = $this->config->get('config_template');
		$this->data['search_keywords'] = $this->config->get('config_search_keywords');
        $this->data['canonical'] = '';
        foreach($this->document->links as $head_link){
            if($head_link['rel'] == 'canonical'){
                $this->data['canonical'] = $head_link['href'];
                break;
            }
        }
		$this->data['addwish_meta_tags'] = array();
        foreach($this->document->links as $head_link){
            if($head_link['rel'] == 'addwish_meta_tags'){
                $this->data['addwish_meta_tags'][] = $head_link['href'];                
            }
        }
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$this->data['base'] = HTTPS_SERVER;
		} else {
			$this->data['base'] = HTTP_SERVER;
		}

		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = $server . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}
		
		$this->data['charset'] = $this->language->get('charset');
		$this->data['lang'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		$this->data['links'] = $this->document->links;	
		$this->data['styles'] = $this->document->styles;
		$this->data['scripts'] = $this->document->scripts;		
		$this->data['breadcrumbs'] = $this->document->breadcrumbs;
		
		$this->data['store'] = $this->config->get('config_name');
		
		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = $server . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}

        $this->data['base_link'] = $this->data['base'] . 'catalog/view/theme/'. $this->config->get('config_template') . '/';

		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_product'] = $this->language->get('text_product');  		     
		$this->data['text_contact'] = $this->language->get('text_contact');     	
    	$this->data['text_blog'] = $this->language->get('text_blog');      	       	
    	$this->data['text_cart'] = $this->language->get('text_cart'); 
    	
    	$this->data['text_omos'] = $this->language->get('text_omos');  
		$this->data['text_handels'] = $this->language->get('text_handels');
    	$this->data['text_checkout'] = $this->language->get('text_checkout');
		$this->data['text_keyword'] = $this->language->get('text_keyword');
		$this->data['text_category'] = $this->language->get('text_category');
		$this->data['text_advanced'] = $this->language->get('text_advanced'); 
		$this->data['entry_search'] = $this->language->get('entry_search');  		
		$this->data['button_go'] = $this->language->get('button_go');
                
		$this->data['home'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');
		
		$this->data['product'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
		if($this->request->get['route']=='product/manufacturer'){
			$this->data['product_active'] = "nav-active";
		}else{
			$this->data['product_active'] = "";
		}
		$this->data['blog'] = 'http://havhoekeren.wordpress.com'; 
		$this->data['contact'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/contact');
		if($this->request->get['route']=='information/contact'){
			$this->data['contact_active'] = "nav-active";
		}else{
			$this->data['contact_active'] = "";
		}
		$this->data['viewcart'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=checkout/cart'); 
		$this->data['manufacturer'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer');
		if($this->request->get['route']=='product/manufacturer'){
			$this->data['manufacturer_active'] = "nav-active";
		}else{
			$this->data['manufacturer_active'] = "";
		}
		
		//$this->load->model('catalog/information');
		$information_id = isset($information['information_id'])?$information['information_id']:0;    		
		//$information = $this->model_catalog_information->getInformationByTitle('Handelsbetingelser'); 		
		//$this->data['handels'] = HTTP_SERVER . 'index.php?route=information/information&information_id=' . $information['information_id'] . '&alias=handelsbetingelser';		
		//$information = $this->model_catalog_information->getInformationByTitle('Om os'); 		
		//$this->data['omos'] = HTTP_SERVER . 'index.php?route=information/information&information_id=' . $information['information_id'] . '&alias=omos';			
		//$this->data['handels'] = HTTP_SERVER . 'index.php?route=information/information&information_id=5&alias=handelsbetingelser';
        $this->data['handels'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/forhandlen');
        if($this->request->get['route']=='information/forhandlen'){
        	$this->data['handels_active'] = "nav-active";
        }else{
        	$this->data['handels_active'] = "";
        }

        
		//$this->data['omos'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/information&information_id=4&alias=omos');			
        $this->data['omos'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/information&information_id=4');
        
        if($this->request->get['route']=='information/information'){
        	$this->data['omos_active'] = "nav-active";
        }else{
        	$this->data['omos_active'] = "";
        }            
        $this->data['news'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=content/list');
		
        $this->load->model('setting/setting');
        $this->data['google_analytics'] = $this->model_setting_setting->getSettingValue('google_analytics', 'google_analytics_code');
        
        $this->load->model('setting/tooltip');              
        $this->data['search_hint'] = $this->model_setting_tooltip->getSearchHint();
            
		//if (isset($this->request->get['keyword'])) {
        if (isset($this->request->post['keyword'])) {
			//$this->data['keyword'] = $this->request->get['keyword'];
            $this->data['keyword'] = $this->request->post['keyword'];
		}
        elseif (isset($this->request->get['keyword'])) {
            $this->data['keyword'] = $this->request->get['keyword'];
        }
        else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->get['category_id'])) {
			$this->data['category_id'] = $this->request->get['category_id'];
		} elseif (isset($this->request->get['path'])) {
			$path = explode('_', $this->request->get['path']);
		
			$this->data['category_id'] = end($path);
		} else {
			$this->data['category_id'] = '';
		}
		
		$this->data['advanced'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/search');
		
		$this->load->model('catalog/category');
        $this->load->model('tool/seo_url');		
		//$this->data['categories'] = $this->getCategories(0);
        //$this->data['categories'] = $this->getParentCategories(0);
        /*
        $categories = $this->getCategories(0);
        echo '<pre>';
        var_dump(array_chunk($categories, 4));
        exit;
        */
        $categories = $this->getCategories(0);
        $total = round(count($categories)/5);
        $this->data['categories'] = array_chunk($categories, $total);
        
        $this->load->model('catalog/manufacturer');
        	
        $this->data['manufacturers'] = array();
        
        $results = $this->model_catalog_manufacturer->getManufacturers();
        
        $manufacturers = array();
        foreach ($results as $result) {
        	$manufacturers[] = array(
                'manufacturer_id' => $result['manufacturer_id'],
                'name'            => $result['name'],
                //'href'            => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer&manufacturer_id=' . $result['manufacturer_id'])
                'href'            => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $result['manufacturer_id'])
        	);
        }
        
        
        $total = round(count($manufacturers)/5);
        
        $this->data['manufacturers'] = array_chunk($manufacturers, $total);      
		
		$this->data['action'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');
        $this->data['search_url'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/search');
        
		if (!isset($this->request->get['route'])) {
			$this->data['redirect'] = HTTP_SERVER . 'index.php?route=common/home';
		} else {
			//$this->load->model('tool/seo_url');
			
			$data = $this->request->get;   			
			unset($data['_route_']);  			
			$route = $data['route'];  			
			unset($data['route']);
			
			$url = '';     			
			if ($data) {
				$url = '&' . urldecode(http_build_query($data));
			}			
			
			$this->data['redirect'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=' . $route . $url);
		}
		
        /*
		$this->data['language_code'] = $this->session->data['language'];     		
		$this->load->model('localisation/language');   		
		$this->data['languages'] = array();            		
		$results = $this->model_localisation_language->getLanguages();       		
		foreach ($results as $result) {
			if ($result['status']) {
				$this->data['languages'][] = array(
					'name'  => $result['name'],
					'code'  => $result['code'],
					'image' => $result['image']
				);	
			}
		}
		        
		$this->data['currency_code'] = $this->currency->getCode();     		
		$this->load->model('localisation/currency');      		 
		$this->data['currencies'] = array();            		 
		$results = $this->model_localisation_currency->getCurrencies();	
		
		foreach ($results as $result) {
			if ($result['status']) {
   				$this->data['currencies'][] = array(
					'title' => $result['title'],
					'code'  => $result['code']
				);
			}
		}
		*/
        
		$this->id = 'header';
		
        $this->children = array(
            'module/cart',
        	'module/cart_detailview',
        	'module/newsletter_home'
        );

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/header.tpl';
		} else {
			$this->template = 'default/template/common/header.tpl';
		}
		
    	$this->render();
	}	
	
    private function getParentCategories($parent_id) { 
        $data = array();
        
        $results = $this->model_catalog_category->getCategories($parent_id);           
        foreach ($results as $result) {     
            $link = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $result['category_id']);
            
            $data[] = array(
                'category_id' => $result['category_id'],
                'name'        => $result['name'],
                'href'        => $this->model_tool_seo_url->rewrite($link),
            );
        }
       
        return $data;
    }
    
	private function getCategories($parent_id, $level = 0) {
		$level++;
		
		if($level > 2) {
			return array();
		}
		$data = array();
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		
		foreach ($results as $result) {
			/* $data[] = array(
				'category_id' => $result['category_id'],
				'name'        => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . $result['name']
			); */

            if($level == 1){
                //$link = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $result['category_id']);
                $link = HTTP_SERVER . 'index.php?route=product/manufacturer/category&amp;path=' . $result['category_id'];
            }
            else{
                $link = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $parent_id . '_' . $result['category_id']);
            }

			$children = $this->getCategories($result['category_id'], $level);
			
			/* if ($children) {
			  $data = array_merge($data, $children);
			} */
			
			$data[] = array(
					'category_id' => $result['category_id'],
					'name'        => $result['name'],
					'href'        => $this->model_tool_seo_url->rewrite($link),
					'children'		=> $children
			);
		}
		
		
		
		return $data;
	}
}
?>